package servlet;

import pojo.Booking;
import service.BookingService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
    BookingService bookingService = new BookingService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        int id = Integer.parseInt(request.getParameter("id"));
        String userName = request.getParameter("userName");
        String cardId = request.getParameter("cardId");
        int status = Integer.parseInt(request.getParameter("status"));
        String bookingDate = request.getParameter("bookingDate");

        Booking bg = new Booking();
        bg.setId(id);
        bg.setUserName(userName);
        bg.setCardId(cardId);
        bg.setStatus(status);
        bg.setBookingDate(bookingDate);

        int result = bookingService.updateBooking(bg);

        if (result>0){
            out.print("<script>alert('修改成功');location.href='ListServlet'</script>");
        }else {
            out.print("<script>alert('修改失败');history.back();</script>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
